// API配置文件
export const API_CONFIG = {
  // 开发环境
  development: {
    baseURL: 'http://localhost:8080/api',
    timeout: 10000,
    withCredentials: true
  },
  
  // 生产环境
  production: {
    baseURL: 'https://api.laundry-platform.com/api',
    timeout: 15000,
    withCredentials: true
  },
  
  // 测试环境
  test: {
    baseURL: 'http://test-api.laundry-platform.com/api',
    timeout: 10000,
    withCredentials: true
  }
}

// 获取当前环境配置
export function getApiConfig() {
  const env = process.env.NODE_ENV || 'development'
  return API_CONFIG[env] || API_CONFIG.development
}

// API端点配置
export const API_ENDPOINTS = {
  // 认证相关
  AUTH: {
    LOGIN: '/auth/login',
    LOGOUT: '/auth/logout',
    REFRESH: '/auth/refresh',
    REGISTER: '/auth/register',
    VERIFY_CODE: '/auth/verify-code',
    RESET_PASSWORD: '/auth/reset-password'
  },

  // 商户相关
  MERCHANT: {
    INFO: '/merchant/info',
    UPDATE: '/merchant/update',
    SETTINGS: '/merchant/settings',
    STATISTICS: '/merchant/statistics',
    VERIFICATION: '/merchant/verification'
  },

  // 仪表板
  DASHBOARD: {
    OVERVIEW: '/dashboard/overview',
    STATISTICS: '/dashboard/statistics',
    RECENT_ORDERS: '/dashboard/recent-orders',
    NOTIFICATIONS: '/dashboard/notifications',
    QUICK_ACTIONS: '/dashboard/quick-actions'
  },

  // 订单管理
  ORDERS: {
    LIST: '/orders',
    DETAIL: '/orders/{id}',
    UPDATE_STATUS: '/orders/{id}/status',
    ACCEPT: '/orders/{id}/accept',
    REJECT: '/orders/{id}/reject',
    PICKUP: '/orders/{id}/pickup',
    PROCESS: '/orders/{id}/process',
    QUALITY_CHECK: '/orders/{id}/quality-check',
    DELIVER: '/orders/{id}/deliver',
    COMPLETE: '/orders/{id}/complete',
    CANCEL: '/orders/{id}/cancel',
    STATISTICS: '/orders/statistics',
    EXPORT: '/orders/export'
  },

  // 财务管理
  FINANCE: {
    OVERVIEW: '/finance/overview',
    BALANCE: '/finance/balance',
    TRANSACTIONS: '/finance/transactions',
    WITHDRAW: '/finance/withdraw',
    BANK_CARDS: '/finance/bank-cards',
    INCOME_STATS: '/finance/income-stats',
    RISK_INFO: '/finance/risk-info',
    FROZEN_DETAILS: '/finance/frozen-details',
    PENDING_DETAILS: '/finance/pending-details'
  },

  // 评价管理
  REVIEWS: {
    LIST: '/reviews',
    DETAIL: '/reviews/{id}',
    REPLY: '/reviews/{id}/reply',
    APPEAL: '/reviews/{id}/appeal',
    STATISTICS: '/reviews/statistics',
    ANALYSIS: '/reviews/analysis',
    TEMPLATES: '/reviews/reply-templates'
  },

  // 服务管理
  SERVICES: {
    LIST: '/services',
    DETAIL: '/services/{id}',
    CREATE: '/services',
    UPDATE: '/services/{id}',
    DELETE: '/services/{id}',
    ACTIVATE: '/services/{id}/activate',
    DEACTIVATE: '/services/{id}/deactivate',
    SUBMIT_REVIEW: '/services/{id}/submit-review',
    STATISTICS: '/services/statistics',
    PROMOTION: '/services/{id}/promotion',
    IMAGES: '/services/{id}/images',
    TEMPLATES: '/services/templates'
  },

  // 营业管理
  BUSINESS: {
    STATUS: '/business/status',
    HOURS: '/business/hours',
    PERFORMANCE: '/business/performance',
    BALANCE: '/business/balance',
    RECHARGE: '/business/recharge',
    PROMOTION: '/business/promotion',
    RISK_ALERTS: '/business/risk/alerts',
    SETTINGS: '/business/settings'
  },

  // 保证金管理
  DEPOSIT: {
    STATISTICS: '/deposit/stats',
    RECORDS: '/deposit/records',
    PAY: '/deposit/pay',
    REFUND: '/deposit/refund',
    REQUIREMENTS: '/deposit/service-requirements',
    PAID_TYPES: '/deposit/paid-service-types',
    PRIORITY: '/deposit/deduction-priority'
  },

  // 设置管理
  SETTINGS: {
    MERCHANT: '/settings/merchant',
    BUSINESS: '/settings/business',
    RISK: '/settings/risk',
    NOTIFICATION: '/settings/notification',
    INTERFACE: '/settings/interface',
    SERVICE_AREAS: '/settings/service-areas',
    WORKING_HOURS: '/settings/working-hours',
    DELIVERY: '/settings/delivery'
  },

  // 统计分析
  STATISTICS: {
    OVERVIEW: '/statistics/overview',
    ORDERS: '/statistics/orders',
    REVENUE: '/statistics/revenue',
    SERVICES: '/statistics/services',
    CUSTOMERS: '/statistics/customers',
    TRENDS: '/statistics/trends'
  },

  // 消息中心
  MESSAGES: {
    LIST: '/messages',
    DETAIL: '/messages/{id}',
    SEND: '/messages',
    MARK_READ: '/messages/{id}/read',
    DELETE: '/messages/{id}',
    STATISTICS: '/messages/statistics'
  },

  // 优惠券管理
  COUPONS: {
    LIST: '/coupons',
    CREATE: '/coupons',
    UPDATE: '/coupons/{id}',
    DELETE: '/coupons/{id}',
    ACTIVATE: '/coupons/{id}/activate',
    DEACTIVATE: '/coupons/{id}/deactivate',
    STATISTICS: '/coupons/statistics'
  },

  // 账户管理
  ACCOUNT: {
    PROFILE: '/account/profile',
    UPDATE_PROFILE: '/account/profile',
    CHANGE_PASSWORD: '/account/change-password',
    UPLOAD_AVATAR: '/account/upload-avatar',
    SECURITY_SETTINGS: '/account/security',
    LOGIN_HISTORY: '/account/login-history'
  },

  // 帮助中心
  HELP: {
    ARTICLES: '/help/articles',
    CATEGORIES: '/help/categories',
    SEARCH: '/help/search',
    FEEDBACK: '/help/feedback',
    FAQ: '/help/faq'
  },

  // 文件上传
  UPLOAD: {
    IMAGE: '/upload/image',
    DOCUMENT: '/upload/document',
    AVATAR: '/upload/avatar',
    SERVICE_IMAGES: '/upload/service-images'
  }
}

// 替换URL中的参数
export function replaceUrlParams(url, params = {}) {
  let result = url
  Object.keys(params).forEach(key => {
    result = result.replace(`{${key}}`, params[key])
  })
  return result
}

// 构建完整的API URL
export function buildApiUrl(endpoint, params = {}) {
  const config = getApiConfig()
  const url = replaceUrlParams(endpoint, params)
  return `${config.baseURL}${url}`
}

// HTTP状态码
export const HTTP_STATUS = {
  OK: 200,
  CREATED: 201,
  NO_CONTENT: 204,
  BAD_REQUEST: 400,
  UNAUTHORIZED: 401,
  FORBIDDEN: 403,
  NOT_FOUND: 404,
  CONFLICT: 409,
  UNPROCESSABLE_ENTITY: 422,
  INTERNAL_SERVER_ERROR: 500,
  BAD_GATEWAY: 502,
  SERVICE_UNAVAILABLE: 503
}

// 业务错误码
export const BUSINESS_ERROR_CODES = {
  // 通用错误
  INVALID_PARAMS: 'INVALID_PARAMS',
  UNAUTHORIZED: 'UNAUTHORIZED',
  FORBIDDEN: 'FORBIDDEN',
  NOT_FOUND: 'NOT_FOUND',
  
  // 认证错误
  INVALID_CREDENTIALS: 'INVALID_CREDENTIALS',
  TOKEN_EXPIRED: 'TOKEN_EXPIRED',
  ACCOUNT_LOCKED: 'ACCOUNT_LOCKED',
  
  // 业务错误
  INSUFFICIENT_BALANCE: 'INSUFFICIENT_BALANCE',
  DEPOSIT_NOT_PAID: 'DEPOSIT_NOT_PAID',
  SERVICE_NOT_AVAILABLE: 'SERVICE_NOT_AVAILABLE',
  ORDER_STATUS_INVALID: 'ORDER_STATUS_INVALID',
  RISK_CONTROL_LIMIT: 'RISK_CONTROL_LIMIT'
}

// 错误消息映射
export const ERROR_MESSAGES = {
  [BUSINESS_ERROR_CODES.INVALID_PARAMS]: '参数错误',
  [BUSINESS_ERROR_CODES.UNAUTHORIZED]: '未授权访问',
  [BUSINESS_ERROR_CODES.FORBIDDEN]: '禁止访问',
  [BUSINESS_ERROR_CODES.NOT_FOUND]: '资源不存在',
  [BUSINESS_ERROR_CODES.INVALID_CREDENTIALS]: '用户名或密码错误',
  [BUSINESS_ERROR_CODES.TOKEN_EXPIRED]: '登录已过期，请重新登录',
  [BUSINESS_ERROR_CODES.ACCOUNT_LOCKED]: '账户已被锁定',
  [BUSINESS_ERROR_CODES.INSUFFICIENT_BALANCE]: '余额不足',
  [BUSINESS_ERROR_CODES.DEPOSIT_NOT_PAID]: '请先缴纳保证金',
  [BUSINESS_ERROR_CODES.SERVICE_NOT_AVAILABLE]: '服务不可用',
  [BUSINESS_ERROR_CODES.ORDER_STATUS_INVALID]: '订单状态无效',
  [BUSINESS_ERROR_CODES.RISK_CONTROL_LIMIT]: '触发风控限制'
}

// 获取错误消息
export function getErrorMessage(errorCode, defaultMessage = '操作失败') {
  return ERROR_MESSAGES[errorCode] || defaultMessage
}
